﻿// CDoctor.cpp: 实现文件
//

#include "pch.h"
#include "HospitalSystem.h"
#include "CDoctor.h"
#include "doctor.h"
#include "patient.h"
// CDoctor

IMPLEMENT_DYNCREATE(CDoctor, CFormView)

CDoctor::CDoctor()
	: CFormView(IDD_CDoctor)
	, m_add1(_T(""))
	, m_add2(_T(""))
	, m_add3(0)
	, m_add4(_T(""))
	, m_add5(_T(""))
	, m_add6(0)
	, m_add7(_T(""))
	, m_xiugai1(_T(""))
	, m_xiugai2(_T(""))
	, m_xiugai3(0)
	, m_xiugai4(_T(""))
	, m_xiugai5(_T(""))
	, m_xiugai6(0)
	, m_xiugai7(_T(""))
	, m_delete(_T(""))
{

}

CDoctor::~CDoctor()
{
}

void CDoctor::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_drlist);
	DDX_Text(pDX, IDC_EDIT2, m_add1);
	DDX_Text(pDX, IDC_EDIT1, m_add2);
	DDX_Text(pDX, IDC_EDIT6, m_add3);
	DDX_Text(pDX, IDC_EDIT3, m_add4);
	DDX_Text(pDX, IDC_EDIT5, m_add5);
	DDX_Text(pDX, IDC_EDIT7, m_add6);
	DDX_Text(pDX, IDC_EDIT14, m_add7);
	DDX_Text(pDX, IDC_EDIT15, m_xiugai1);
	DDX_Text(pDX, IDC_EDIT8, m_xiugai2);
	DDX_Text(pDX, IDC_EDIT11, m_xiugai3);
	DDX_Text(pDX, IDC_EDIT10, m_xiugai4);
	DDX_Text(pDX, IDC_EDIT12, m_xiugai5);
	DDX_Text(pDX, IDC_EDIT13, m_xiugai6);
	DDX_Text(pDX, IDC_EDIT16, m_xiugai7);
	//  DDX_Text(pDX, IDC_EDIT9, m_delete);
	DDX_Text(pDX, IDC_EDIT9, m_delete);
}

BEGIN_MESSAGE_MAP(CDoctor, CFormView)
	ON_BN_CLICKED(IDC_BUTTON5, &CDoctor::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON6, &CDoctor::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON7, &CDoctor::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON8, &CDoctor::OnBnClickedButton8)
	ON_BN_CLICKED(IDC_BUTTON9, &CDoctor::OnBnClickedButton9)
	ON_BN_CLICKED(IDC_BUTTON10, &CDoctor::OnBnClickedButton10)
END_MESSAGE_MAP()


// CDoctor 诊断

#ifdef _DEBUG
void CDoctor::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void CDoctor::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// CDoctor 消息处理程序


void CDoctor::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	CString field[] = { L"工作证号",L"姓名",L"年龄",L"性别",L"职称" ,L"工资信息",L"部门编号" };
	for (int i = 0; i < sizeof(field) / sizeof(field[0]); ++i)
	{
		m_drlist.InsertColumn(i, field[i], LVCFMT_CENTER, 90);
	}
	//设置正文
	CString strsql;
	strsql.Format(L"select * from doctor");
	List_All(strsql);

	// TODO: 在此添加专用代码和/或调用基类
}


void CDoctor::OnBnClickedButton5()
{
	// TODO: 在此添加控件通知处理程序代码
	Doctor m_cd;
	if (m_cd.IsOpen())
	{
		m_cd.Close();
	}
	m_cd.Open(CRecordset::dynaset);
		UpdateData(true);
		m_cd.MoveFirst();
	while (!m_cd.IsEOF())
	{
		
		if (m_add1 == m_cd.m_dr_id)
		{
			MessageBox(L"改医生已存在！");
			return;
		}
		m_cd.MoveNext();
	}
	m_cd.AddNew();
	m_cd.m_dr_id = m_add1;
	m_cd.m_dr_name = m_add2;
	m_cd.m_dr_age = m_add3;
	m_cd.m_dr_sex = m_add4;
	m_cd.m_dr_zc = m_add5;
	m_cd.m_dr_wage = m_add6;
	m_cd.m_dep_id = m_add7;
	if (m_cd.Update())
	{
		MessageBox(L"添加成功！");
		CString strsql;
		strsql.Format(L"select * from doctor");
		List_All(strsql);
	}
	else
	{
		MessageBox(L"添加失败！");
	}
	
	m_cd.Close();
}


void CDoctor::OnBnClickedButton6()
{
	// TODO: 在此添加控件通知处理程序代码
	m_add2.Empty();
	m_add1.Empty();
	m_add4.Empty();
	m_add5.Empty();
	m_add7.Empty();
	UpdateData(FALSE); //把数据更新到控件上
}


void CDoctor::OnBnClickedButton7()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(true);
	Doctor m_cd;
	m_cd.Open(CRecordset::dynaset);
	if (!m_cd.CanUpdate())
	{
		MessageBox(L"无权限！");
		return;
	}
	m_cd.MoveFirst();
	while (!m_cd.IsEOF())
	{
		m_cd.MoveNext();
		if (m_xiugai1 == m_cd.m_dr_id)
		{
			m_cd.Edit();
			m_cd.m_dr_name = m_xiugai2;
			m_cd.m_dr_age = m_xiugai3;
			m_cd.m_dr_sex = m_xiugai4;
			m_cd.m_dr_zc = m_xiugai5;
			m_cd.m_dr_wage = m_xiugai6;
			m_cd.m_dep_id = m_xiugai7;
			m_cd.Update();
			MessageBox(L"修改成功");
			CString strsql;
			strsql.Format(L"select * from doctor");
			List_All(strsql);
			return;
		}
	}

	MessageBox(L"不存在医生的信息！");
}


void CDoctor::OnBnClickedButton8()
{
	// TODO: 在此添加控件通知处理程序代码
	m_xiugai2.Empty();
	m_xiugai1.Empty();
	m_xiugai4.Empty();
	m_xiugai5.Empty();
	m_xiugai7.Empty();
	UpdateData(FALSE); //把数据更新到控件上
}


void CDoctor::OnBnClickedButton9()
{
	// TODO: 在此添加控件通知处理程序代码
	Doctor m_cd;
	Patient pt;
	m_cd.Open(CRecordset::dynaset);
	pt.Open(CRecordset::dynaset);
	UpdateData(true);
	m_cd.MoveFirst();
	pt.MoveFirst();
	while (!pt.IsEOF())
	{
		if (pt.m_dr_id == m_delete)
		{
			pt.Close();
			m_cd.Close();
			MessageBox(L"改医生还负责几个患者，不能删除！");
			return;
		}
		pt.MoveNext();
	}
	while (!m_cd.IsEOF())
	{
		if (m_cd.m_dr_id == m_delete)
		{
			m_cd.Delete();
			m_cd.Close();
			MessageBox(L"删除成功！");
			CString strsql;
			strsql.Format(L"select * from doctor");
			List_All(strsql);
			return;
		}
		m_cd.MoveNext();
	}
	MessageBox(L"不存在医生，删除失败！");
}


void CDoctor::OnBnClickedButton10()
{
	// TODO: 在此添加控件通知处理程序代码
	m_delete.Empty();
	UpdateData(FALSE); //把数据更新到控件上
}
void CDoctor::List_All(CString str=L"select * from doctor") {
	m_drlist.DeleteAllItems();
	//Cscore m_score;
	Doctor m_dr;
	try {
		if (m_dr.IsOpen())
			m_dr.Close();
		if (!m_dr.Open(CRecordset::snapshot, str)) {
			MessageBox(L"打开数据库失败", L"数据库错误", MB_OK);
			return;
		}
	}
	catch (CDBException* e) {
		e->ReportError();
	}

	int nindex = 0;
	m_dr.MoveFirst();
	CString age, wage;
	while (!m_dr.IsEOF()) {
		LV_ITEM litem;
		litem.mask = LVIF_TEXT;
		litem.iItem = nindex;
		litem.iSubItem = 0;
		litem.pszText = L"";
		m_drlist.InsertItem(&litem);
		age.Format(L"%3d", m_dr.m_dr_age);
		wage.Format(L"%3d", m_dr.m_dr_wage);
		m_drlist.SetItemText(nindex, 0, m_dr.m_dr_id);
		m_drlist.SetItemText(nindex, 1, m_dr.m_dr_name);
		m_drlist.SetItemText(nindex, 2, age);
		m_drlist.SetItemText(nindex, 3, m_dr.m_dr_sex);
		m_drlist.SetItemText(nindex, 4, m_dr.m_dr_zc);
		m_drlist.SetItemText(nindex, 5, wage);
		m_drlist.SetItemText(nindex, 6, m_dr.m_dep_id);
		m_dr.MoveNext();
		nindex++;
	}
	m_dr.Close();
}

